import os
import platform

from seleniumwire import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

current_path = os.getcwd()  # 当前文件所在的文件夹路径
# 指定谷歌的位置
chrome_location = ''
# Chrome浏览器的位置
browser_location = ''
# ChromeDriver的位置
driver_location = ''
#
os_name = platform.system()

if os_name.lower() == 'windows':
    # 指定谷歌的位置
    chrome_location = os.path.join(current_path, 'chrome-win')
    # ChromeDriver的位置
    driver_location = os.path.join(chrome_location, 'chromedriver.exe')
elif os_name.lower() == 'linux':
    # 指定谷歌的位置
    chrome_location = os.path.join(current_path, 'chrome-linux')
    # ChromeDriver的位置
    driver_location = os.path.join(chrome_location, 'chromedriver')
    browser_location = os.path.join(chrome_location, 'chrome')

else:
    print('当前操作系统为:{}'.format(os_name))

# 创建一个Servic对象，传入ChromeDriver的路径
service = Service(driver_location)
# 创建Chrome选项
options = Options()
if os_name.lower() == 'linux':
    # option设置，传入Chrome浏览器的路径
    options.binary_location = browser_location
    print('options.binary_location:{}'.format(browser_location))

# # 当前文件所在的文件夹路径
# current_path = os.getcwd()
# # chrome浏览器路径
# chrome_location = os.path.join(current_path, 'chrome-win')
# # chrome.exe完整路径
# browser_location = os.path.join(chrome_location, 'chrome.exe')
# # chromedriver.exe的完整路径
# driver_location = os.path.join(chrome_location, 'chromedriver.exe')
# # 创建一个Servic对象，传入ChromeDriver的路径
# service = Service(driver_location=driver_location)
# # 创建Chrome选项
# options = Options()
# # options.add_argument("--headless") # 无头模式(无界面)
# # 禁用图片
# # options.add_argument('blink-settings=imagesEnabled=false')
# # option设置，传入Chrome浏览器的路径(chrome.exe完整路径)
# options.binary_location = browser_location
# # 不显示 Chrom正受到自动测试软件控制
options.add_experimental_option("excludeSwitches", ['enable-automation'])
# # # selenium-wire使用代理方式一
# # seleniumwire_options = {
# #     'proxy': {
# #         'http': 'http://117.69.37.46:8090',  # 这里使用自己的代理ip和端口
# #         'https': 'https://117.69.37.46:8090',  # 这里使用自己的代理ip和端口
# #         'no_proxy': 'localhost,127.0.0.1'
# #     }
# # }
# 关闭webrtc
preferences = {
    "webrtc.ip_handling_policy": "disable_non_proxied_udp",
    "webrtc.multiple_routes_enabled": False,
    "webrtc.nonproxied_udp_enabled": False
}
options.add_experimental_option("prefs", preferences)
# 创建 Chrome 浏览器驱动对象
driver = webdriver.Chrome(service=service, options=options)
# selenium-wire使用代理方式二
# ip_port = 'http://56.111.111.111:10380'
# driver.proxy = {
#     'http': f'{ip_port}',
#     'https': f'{ip_port}'
# }
# driver.set_page_load_timeout(15) # 设置页面加载超时时间为15秒
# driver.set_script_timeout(15)  # 设置js加载超时时间为15秒
try:
    url = "https://www.creditchina.gov.cn/"
    driver.get(url)
    print(driver.page_source)
except Exception as e:
    print(e)
finally:
    driver.quit()
